package com.definesys.smecrailtransit.module.TotalTime;

import android.content.Context;

import com.definesys.smecrailtransit.SmecManager.SmecRetrofit.BaseSubscriber;
import com.definesys.smecrailtransit.SmecManager.SmecRetrofit.HttpResult;
import com.definesys.smecrailtransit.api.SmecRailApi;
import com.definesys.smecrailtransit.base.SmecBasePresenter;
import com.definesys.smecrailtransit.bean.ApplyBean;
import com.definesys.smecrailtransit.bean.LineBean;
import com.definesys.smecrailtransit.bean.StationBean;
import com.definesys.smecrailtransit.module.TotalTime.bean.DateTimeBean;
import com.definesys.smecrailtransit.module.TotalTime.bean.ElevatorBean;
import com.definesys.smecrailtransit.module.TotalTime.bean.TotalTimeBean;
import com.definesys.smecrailtransit.module.TotalTime.dialog.TotalTimeDialog;

import java.util.ArrayList;
import java.util.HashMap;

import rx.Observable;

/**
 * Created by cjy on 2017/8/14.
 */

public class TotalTimePresenter extends SmecBasePresenter implements TotalTimeContract{

    public TotalTimePresenter(Context mContext) {
        super(mContext);
    }


    @Override
    public void getLineTime(HashMap<String, Object> map) {
        Observable<HttpResult<TotalTimeBean>> observable=smecRailService.lineTime(map);
        schedulerThread(observable).subscribe(new BaseSubscriber<HttpResult<TotalTimeBean>>() {
            @Override
            public void onError(Throwable e) {
                super.onError(e);
                mRxbus.post(TotalTimeFragment.GET_TOTAL_TIME_FAILED,"");
            }

            @Override
            public void onNext(HttpResult<TotalTimeBean> arrayListHttpResult) {
                TotalTimeBean arrayList=arrayListHttpResult.getData();
                ArrayList<LineBean> lineList = arrayList.getLine();
                ArrayList<ApplyBean> applyList = arrayList.getApply();
                ArrayList<StationBean> stationList = arrayList.getStation();

                for (LineBean lineBean : lineList) {
                    lineBean.setFlag(SmecRailApi.MonthCode.TOTALTIME);
                }
                for (ApplyBean applyBean : applyList) {
                    applyBean.setFlag(SmecRailApi.MonthCode.TOTALTIME);
                }
                for (StationBean stationBean : stationList) {
                    stationBean.setFlag(SmecRailApi.MonthCode.TOTALTIME);
                }

                realmManager.insertOrUpdateBatch(lineList);
                realmManager.insertOrUpdateBatch(applyList);
                realmManager.insertOrUpdateBatch(stationList);
                mRxbus.post(TotalTimeFragment.GET_TOTAL_TIME_SUCCESS,"");
            }
        });
    }

    @Override
    public void getStationTime(HashMap<String, Object> map) {
        Observable<HttpResult<ArrayList<ElevatorBean>>> observable=smecRailService.stationTime(map);
        schedulerThread(observable).subscribe(new BaseSubscriber<HttpResult<ArrayList<ElevatorBean>>>() {
            @Override
            public void onError(Throwable e) {
                super.onError(e);
                mRxbus.post(TotalTimeDialog.GET_STATION_TIME_FAILED,"");
            }

            @Override
            public void onNext(HttpResult<ArrayList<ElevatorBean>> arrayListHttpResult) {
                ArrayList<ElevatorBean> list = arrayListHttpResult.getData();
                mRxbus.post(TotalTimeDialog.GET_STATION_TIME_SUCCESS,list);
            }
        });
    }

    @Override
    public void getElevatorTime(HashMap<String, Object> map) {
        Observable<HttpResult<ArrayList<DateTimeBean>>> observable=smecRailService.elevatorTime(map);
        schedulerThread(observable).subscribe(new BaseSubscriber<HttpResult<ArrayList<DateTimeBean>>>() {
            @Override
            public void onError(Throwable e) {
                super.onError(e);
                mRxbus.post(TotalTimeDialog.GET_ELEVATOR_TIME_FAILED,"");
            }

            @Override
            public void onNext(HttpResult<ArrayList<DateTimeBean>> arrayListHttpResult) {
                ArrayList<DateTimeBean> list = arrayListHttpResult.getData();
                mRxbus.post(TotalTimeDialog.GET_ELEVATOR_TIME_SUCCESS,list);
            }
        });
    }
}
